function rr=detect_RR(G,sample,timestamp)
RRINTERVAL_WINDOW=10*60*1000; % 10 minute

rr.sample=[];
rr.timestamp=[];

for t=timestamp(1):RRINTERVAL_WINDOW:timestamp(end)
    selected=find(timestamp>=t & timestamp<=t+RRINTERVAL_WINDOW);
    ecgS=sample(selected);
    ecgT=timestamp(selected);
    if size(ecgS,2)==0
        continue;
    end
    [pk,x4]=rpeaks(ecgS);
    pkT=ecgT(pk);
    
    rrV=diff(pkT);
    rrT=pkT(1:end-1);
    
    if isempty(rrV)
        continue;
    end
    rrV=rrV/1000;
    %% Save RR interval
    rr.sample=[rr.sample,rrV];
    rr.timestamp=[rr.timestamp,rrT];
end
end
